package com.ctg.behavior.calc.controller.bookmark;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.ctg.behavior.calc.utils.CtFileUtils;
import com.ctg.behavior.common.CtgIdData;
import com.ctg.behavior.common.CtgIdResult;
import com.ctg.behavior.common.CtgRowsResult;
import com.ctg.behavior.common.CtgSingleResult;
import com.ctg.behavior.common.bookmark.CtBookmarkBody;
import com.ctg.behavior.common.bookmark.CtBookmarkCreateBody;
import com.ctg.behavior.common.bookmark.CtBookmarkUpdateBody;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("/api/bookmarks/bookmark")
public class BookmarkController {

	@Value("${simulator.path}")
	private String simlutorPath;

	@PostMapping("/create")
	public CtgIdResult bookmarkCreate(@RequestBody CtBookmarkCreateBody body) throws Exception {

		CtgIdResult result = new CtgIdResult();
		log.info("body={}", body);

		Long theCreateFileNumber = null;
		String filePath = simlutorPath + "/bookmarks/bookmark";
		FileUtils.forceMkdir(new File(filePath));
		List<Long> fileNumbers = CtFileUtils.listFileNumbers(filePath);
		if (!fileNumbers.isEmpty()) {
			theCreateFileNumber = fileNumbers.get(fileNumbers.size() - 1);
			theCreateFileNumber += 1;
		} else {
			theCreateFileNumber = 100001L;
		}

		CtBookmarkBody detailBody = JSON.parseObject(JSON.toJSONString(body), CtBookmarkBody.class);
		detailBody.setId(theCreateFileNumber);

		String targetFile = filePath + "/" + theCreateFileNumber + ".json";
		FileUtils.writeStringToFile(new File(targetFile), JSON.toJSONString(detailBody), "UTF-8");

		result.setErrorMessage("created successfully!");
		CtgIdData idData = new CtgIdData();
		idData.setId(theCreateFileNumber);
		result.setValue(idData);

		return result;
	}

	@PostMapping("/update")
	public CtgIdResult bookmarkUpdate(@RequestParam Long id, @RequestBody CtBookmarkUpdateBody body) throws Exception {

		CtgIdResult result = new CtgIdResult();
		log.info("body={}", body);

		Long theCreateFileNumber = body.getId();
		String filePath = simlutorPath + "/bookmarks/bookmark";
		FileUtils.forceMkdir(new File(filePath));
		CtBookmarkUpdateBody detailBody = body;

		String targetFile = filePath + "/" + theCreateFileNumber + ".json";
		FileUtils.writeStringToFile(new File(targetFile), JSON.toJSONString(detailBody), "UTF-8");

		result.setErrorMessage("updated successfully!");
		CtgIdData idData = new CtgIdData();
		idData.setId(theCreateFileNumber);
		result.setValue(idData);

		return result;
	}

	@GetMapping("/detail")
	public CtgSingleResult<CtBookmarkBody> bookmarkDetail(@RequestParam Long id) throws Exception {

		CtgSingleResult<CtBookmarkBody> result = new CtgSingleResult<>();
		log.info("id={}", id);

		Long theCreateFileNumber = id;
		String filePath = simlutorPath + "/bookmarks/bookmark";
		FileUtils.forceMkdir(new File(filePath));

		String bodyFile = filePath + "/" + theCreateFileNumber + ".json";
		String body = FileUtils.readFileToString(new File(bodyFile), "UTF-8");
		CtBookmarkBody detailData = JSON.parseObject(body, CtBookmarkBody.class);
		result.setValue(detailData);

		result.setErrorMessage("get successfully!");

		return result;
	}

	@GetMapping("/list")
	public CtgRowsResult<CtBookmarkBody> bookmarkList(@RequestParam Long limit) throws Exception {

		CtgRowsResult<CtBookmarkBody> result = new CtgRowsResult<>();
		log.info("limit={}", limit);

		String filePath = simlutorPath + "/bookmarks/bookmark";
		FileUtils.forceMkdir(new File(filePath));

		List<CtBookmarkBody> bookmarkBodyList = new ArrayList<>();
		result.getValue().setList(bookmarkBodyList);

		List<Long> fileNumbers = CtFileUtils.listFileNumbers(filePath);
		for (Long oneItem : fileNumbers) {
			Long theCreateFileNumber = oneItem;
			String bodyFile = filePath + "/" + theCreateFileNumber + ".json";
			String body = FileUtils.readFileToString(new File(bodyFile), "UTF-8");
			CtBookmarkBody bookmarkData = JSON.parseObject(body, CtBookmarkBody.class);
			bookmarkBodyList.add(bookmarkData);
//			bookmarkData.setsetCreateTime(CtDateUtils.getCurrentDate());
//			bookmarkData.setUpdateTime(CtDateUtils.getCurrentDate());
		}

		result.setErrorMessage("list successfully!");

		return result;
	}
	
}
